package ru.stepanovdn.trading.server.service.candle

import ru.tinkoff.piapi.contract.v1.Quotation
import java.math.BigDecimal
import java.math.RoundingMode
import java.time.Instant
import java.time.OffsetDateTime
import java.time.ZoneOffset

object ConvertorUtils {
    fun toBigDecimal(value: Quotation?, scale: Int?): BigDecimal {
        if (value == null) {
            return BigDecimal.ZERO
        }
        val result = BigDecimal.valueOf(value.units).add(BigDecimal.valueOf(value.nano.toLong(), 9))
        if (scale != null) {
            return result.setScale(scale, RoundingMode.HALF_EVEN)
        }
        return result
    }

    fun toOffsetDateTime(seconds: Long): OffsetDateTime {
        return Instant.ofEpochSecond(seconds).atOffset(ZoneOffset.UTC)
    }
}
